package edu.unlv.cs.ladders.services;

import org.springframework.stereotype.Service;

import edu.unlv.cs.ladders.entities.*;

@Service
public class EventServiceImpl implements EventService {

	public void setWinner(Event event, int challengerScore,
			int defenderScore) {
        if(challengerScore!=defenderScore){
	        Team challenger = event.getChallenger();
	        Team defender = event.getDefender();
	        
	        int winnerRank = Math.min(challenger.getRank(), defender.getRank());
	        int looserRank = Math.max(challenger.getRank(), defender.getRank());
	        
	        if(challengerScore>defenderScore){
	        	// challenger won, they get the higher position
	        	challenger.setRank(winnerRank);
	        	defender.setRank(looserRank);
	        } else if(defenderScore>challengerScore){
	        	// defender won, they get the higher position
	        	defender.setRank(winnerRank);
	        	challenger.setRank(looserRank);
	        }
	        challenger.merge();
	        defender.merge();
        }     
	}

}
